using System;
using System.Collections.Generic;
using System.Security.Cryptography;
using System.Text;
using System.Data;

using PlugNT.Safe;
using PlugNT.Database;
using PlugNT.Database.Common.Simple;
using PlugNT.Cms.Custom;
using PlugNT.App.UCenter.Model;

namespace PlugNT.App.UCenter.DAL
{
    public class UIntegral 
    {
        private static string currTableName = WebConfig.TablePrefix + "u_integral";



        public UIntegralInfo GetUIntegralInfo(int id)
        {
            UIntegralInfo model = null;
            IDataReader dr = GetReader(1, "*", " and id=" + id.ToString());
            if (dr != null)
            {
                if (dr.Read())
                {
                    model = new UIntegralInfo();
                    model.id = int.Parse(dr["id"].ToString());
                    model.admin_id = int.Parse(dr["admin_id"].ToString());
                    model.username = dr["username"].ToString();
                    model.operate = int.Parse(dr["operate"].ToString());
                    model.remark = dr["remark"].ToString();
                    model.create_time = DateTime.Parse(dr["create_time"].ToString());
                    model.direction = int.Parse(dr["direction"].ToString());
                }
                dr.Close();
            }
            return model;
        }

        public bool Add(UIntegralInfo model)
        {
            StringBuilder strSql = new StringBuilder();
 

            strSql.Append("insert into " + currTableName + " (");
            strSql.Append("admin_id,username,[operate],");
            strSql.Append("[remark],create_time,[direction])");
            strSql.Append(" values ( ");
            strSql.Append(" " + model.admin_id.ToString() + ",");
            strSql.Append("'" + StringHelper.SqlFilter(model.username) + "',");
            strSql.Append(" " + model.operate.ToString() + ",");
            strSql.Append("'" + StringHelper.SqlFilter(model.remark) + "',");

            strSql.Append("'" + model.create_time.ToString() + "',");
            strSql.Append(" " + model.direction.ToString());
            strSql.Append(" ) ");

            return DbHelper.ExecuteSql(strSql.ToString()) > 0;
        }

        public bool Delete(string andWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from " + currTableName);
            strSql.Append(" where 1=1 " + andWhere);

            return DbHelper.ExecuteSql(strSql.ToString()) > 0;
        }





        public DataTable GetList( string andWhere)
        {
            return DbHelper.GetDataTable(string.Format("select * from [" + currTableName + "] where 1=1 {0}", andWhere));
        }


        public DataTable GetList( int topCount, string andWhere)
        {
            string tmpTopCount = (topCount <= 0) ? "" : " top " + topCount.ToString();
            return DbHelper.GetDataTable(string.Format("select {0} * from [" + currTableName + "] where 1=1 {1}", tmpTopCount, andWhere));

        }

        public DataTable GetList(int topCount, int currPageCount, string andWhere)
        {
            string sql = Sql.GetRePagingString(topCount, currPageCount, currTableName, "id",  andWhere);
            return DbHelper.GetDataTable(sql);
        }

        public DataTable GetList( int topCount, int currPageCount, string colNames, string andWhere)
        {
            string sql = Sql.GetRePagingString(topCount, currPageCount, currTableName, "id", colNames,  andWhere);
            return DbHelper.GetDataTable(sql);
        }

        public DataTable GetList( int topCount, int currPageCount, string andWhere, LeftJoinInfo joininfo)
        {
            string sql = Sql.GetRePagingString(topCount, currPageCount, currTableName, "id", "*",  andWhere, joininfo);

            return DbHelper.GetDataTable(sql);
        }


        public int GetCount( string andWhere)
        {
            string sql = "select count(1) from [" + currTableName + "] where 1=1 "  + andWhere;
            object obj = DbHelper.GetSingle(sql);
            return obj == null ? 0 : int.Parse(obj.ToString());
        }


        private static IDataReader GetReader(int topCount, string colNames, string andWhere)
        {
            string sql = "select " + (topCount == -1 ? "" : "top " + topCount) + " " + colNames + " from " + currTableName + " where 1=1 " + andWhere;
            return DbHelper.ExecuteReader(sql);
        }



    }
}

